package com.qf.wangqing.sportsnews.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.qf.wangqing.sportsnews.BR;
import com.qf.wangqing.sportsnews.MyApplication;
import com.qf.wangqing.sportsnews.R;
import com.qf.wangqing.sportsnews.adapter.recommendadapter.TitleCommentRecyclerAdapter;
import com.qf.wangqing.sportsnews.bean.newstitletype4.TitleChilds;
import com.qf.wangqing.sportsnews.bean.newstitletype4.TitleContents;
import com.qf.wangqing.sportsnews.bean.newstitletype4.TitleContest;
import com.qf.wangqing.sportsnews.bean.newstitletype4.TitleData;
import com.qf.wangqing.sportsnews.bean.newstitletype4.TitleRoot;
import com.qf.wangqing.sportsnews.databinding.ActivityTitleType4Binding;
import com.qf.wangqing.sportsnews.utils.DividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TitleType4Activity extends AppCompatActivity implements Callback<TitleRoot>, View.OnClickListener {
    private ActivityTitleType4Binding binding;
    private Map<Class<?>,Pair<Integer,Integer>> map;
    private TitleCommentRecyclerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_title_type4);

        init();

        Intent intent = getIntent();
        String link = intent.getStringExtra("link");
        Log.i("link4","link4"+link);
        MyApplication myApplication = (MyApplication) getApplication();

        myApplication.getNewsService().getType4(link,"json","4","false").enqueue(this);
    }
    public void init(){
        map = new HashMap();
        map.put(TitleData.class,Pair.create(R.layout.titleitem, BR.digest));
        map.put(TitleContest.class,Pair.create(R.layout.title_contestitem,BR.contest));
        map.put(TitleChilds.class,Pair.create(R.layout.titlechilditem,BR.child));
        map.put(TitleContents.class,Pair.create(R.layout.titleitem_long,BR.contents));
        adapter = new TitleCommentRecyclerAdapter(this,new ArrayList(),map);
        binding.recycler.setAdapter(adapter);
        binding.recycler.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
        //返回键
        binding.back.setOnClickListener(this);
    }

    @Override
    public void onResponse(Call<TitleRoot> call, Response<TitleRoot> response) {
//        Log.i("body",response.body().getData().getFullImageUrl());
        TitleRoot body = response.body();

        if (body.getData() != null) {
            //需要把数据set进实体类
            binding.setData(body.getData());
            adapter.add(0,body.getData());
        }
        if (body.getData().getContest_content().getContest() != null) {
            adapter.add(1,body.getData().getContest_content().getContest());
        }
        if (body.getData().getChilds().get(0) != null) {
            adapter.add(2,body.getData().getChilds().get(0));
        }
        if (body.getData().getChilds().get(0).getContents() != null && body.getData().getChilds().get(0).getContents().size() >0) {
            adapter.addAll(body.getData().getChilds().get(0).getContents());
        }
        if (body.getData().getChilds().get(1) != null) {
            adapter.add(body.getData().getChilds().get(1));
        }
        if (body.getData().getChilds().get(1).getContents() != null && body.getData().getChilds().get(1).getContents().size() >0) {
            adapter.addAll(body.getData().getChilds().get(1).getContents());
        }


    }

    @Override
    public void onFailure(Call<TitleRoot> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        finish();
        overridePendingTransition(R.anim.left, R.anim.exit);
    }
}
